﻿using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SGSN_try
{
    class Read
    {
        public static string file;

        public Read(string filename)
        {
            file = filename;
        }

        public static string swapHex(FileStream stream, int startIndex, int length, int loop)
        {
            BinaryReader br = new BinaryReader(stream);
            StringBuilder sb = new StringBuilder();
            stream.Seek(startIndex + loop * 490, SeekOrigin.Begin);
            for (int i = 1; i <= length; i++)
            {
                sb.Append(BinaryProperties.swap(BitConverter.ToString(br.ReadBytes(1))));
            }
            return sb.ToString();
        }
        public static string processArray(FileStream stream, int startIndex, int length, int loop)
        {
            BinaryReader br = new BinaryReader(stream);
            StringBuilder sb = new StringBuilder();
            stream.Seek(startIndex + loop * 490, SeekOrigin.Begin);
            string a = BitConverter.ToString(br.ReadBytes(startIndex + 1), startIndex).ToString();
            return a;
        }
        public static string readLittleEndian(FileStream stream, int startIndex, int length, int loop)
        {
            BinaryReader br = new BinaryReader(stream);
            StringBuilder sb = new StringBuilder();
            stream.Seek(startIndex + loop * 490, SeekOrigin.Begin);
            string a = BitConverter.ToUInt16(br.ReadBytes(length + startIndex), startIndex).ToString();
            return a; 
        }

        public static string joinResults()
        {
            using (FileStream stream = new FileStream(file, FileMode.Open))
            {                
                BinaryReader br = new BinaryReader(stream);
                StringBuilder sb = new StringBuilder();
                sb.AppendFormat("{0}\t{1}\t{2}\t{3}\t{4}\t{5}{6}{7}{8}{9}{10}", "CDRLength", "IMSI", "IMEI", "MSISDN", "SGSN", /*"GGSN", "PDP",*/ "LAC", "RA", "GGSN",
                    Environment.NewLine, "----------------------------------------------------", Environment.NewLine);
                for (int j = 0; j < BinaryProperties.cdrLength; j++)
                {
                    sb.AppendFormat("{0}\t{1}\t{2}\t{3}\t{4}\t{5}\t{6}\t{7}\t{8}", readLittleEndian(stream, BinaryProperties.startIndex_cdrLenght, BinaryProperties.lenght_cdrLenght, j) ,                        
                        swapHex(stream, BinaryProperties.startIndex_IMSI, BinaryProperties.length_IMSI, j),
                        swapHex(stream, BinaryProperties.startIndex_IMEI, BinaryProperties.length_IMEI, j), 
                        swapHex(stream, BinaryProperties.startIndex_MSISDN, BinaryProperties.length_MSISDN, j), 
                        processArray(stream,BinaryProperties.startIndex_sgsn, BinaryProperties.length_sgsn, j),
                        readLittleEndian(stream, BinaryProperties.startIndex_LAC, BinaryProperties.length_LAC,j),
                        swapHex(stream, BinaryProperties.startIndex_routingArea, BinaryProperties.length_routingArea,j),
                        processArray(stream, BinaryProperties.length_ggsn, BinaryProperties.startIndex_ggsn, j),
                        Environment.NewLine).Replace("F", String.Empty);
                }
                return sb.ToString();
            }           
        } 
    }
}
